
package com.insigma.business.qrcode.merchant._provider.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantEvaluationProviderConverter;
import com.insigma.business.qrcode.common.dto.CommonFavoriteDO;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.pay.dto.CareWelfareMerchantEvaluationCountDTO;
import com.insigma.business.qrcode.pay.dto.CareWelfareMerchantEvaluationDTO;
import com.insigma.business.qrcode.pay.dto.CareWelfareMerchantEvaluationSearchRequest;
import com.insigma.business.qrcode.pay.entity.MerchantOrder;
import com.insigma.business.qrcode.pay.service.MerchantOrderService;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.merchant.dto.search.CareWelfareMerchantEvaluationBSearcher;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantEvaluationDO;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantEvaluationInsertRequest;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantEvaluationUpdateRequest;
import com.insigma.business.qrcode.merchant._provider.CareWelfareMerchantEvaluationProvider;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantEvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("careWelfareMerchantEvaluationProvider")
public class CareWelfareMerchantEvaluationProviderImpl extends BaseProviderImpl implements CareWelfareMerchantEvaluationProvider {

    @Autowired
    private CareWelfareMerchantEvaluationService careWelfareMerchantEvaluationService;

    @Autowired
    private MerchantOrderService merchantOrderService;


    @Override
    public BasePagingResponse<List<CareWelfareMerchantEvaluationDTO>> searchCareWelfareMerchantEvaluation(final CareWelfareMerchantEvaluationSearchRequest searchRequest) {

        final BasePagingResponse<List<CareWelfareMerchantEvaluationDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchantEvaluation的请求参数不能为空");


            }

            @Override
            public void executeService() {
                PageHelper.startPage(searchRequest.getPageNo(), searchRequest.getPageSize());

                final CareWelfareMerchantEvaluationBSearcher careWelfareMerchantEvaluationBSearcher = CareWelfareMerchantEvaluationProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CareWelfareMerchantEvaluationDO> careWelfareMerchantEvaluationDOList = careWelfareMerchantEvaluationService.search(careWelfareMerchantEvaluationBSearcher);
                PageInfo<CareWelfareMerchantEvaluationDO> pageInfo = new PageInfo<>(careWelfareMerchantEvaluationDOList);
                final List<CareWelfareMerchantEvaluationDTO> careWelfareMerchantEvaluationDTOList = CareWelfareMerchantEvaluationProviderConverter.convertResponseDOList2DTOList(careWelfareMerchantEvaluationDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantEvaluationDTOList, searchRequest,pageInfo.getTotal());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CareWelfareMerchantEvaluationDTO> getCareWelfareMerchantEvaluation(final BaseGetRequest baseGetRequest){

        final BaseResponse<CareWelfareMerchantEvaluationDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CareWelfareMerchantEvaluation的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CareWelfareMerchantEvaluationDO careWelfareMerchantEvaluationDO = careWelfareMerchantEvaluationService.get((String)baseGetRequest.getId());
                final CareWelfareMerchantEvaluationDTO remoteObj = CareWelfareMerchantEvaluationProviderConverter.convertResponseDO2DTO(careWelfareMerchantEvaluationDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCareWelfareMerchantEvaluation(final CareWelfareMerchantEvaluationInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CareWelfareMerchantEvaluation的请求参数不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getMerchantId(), "商家id请求参数不能为空");
            }
            @Override
            public void executeService() {
                final CareWelfareMerchantEvaluationDO careWelfareMerchantEvaluationDO = CareWelfareMerchantEvaluationProviderConverter.convertRequestIO2DO(insertRequest);
                careWelfareMerchantEvaluationService.insert(careWelfareMerchantEvaluationDO);

                String orderId = insertRequest.getParentId();
                if (!StringUtil.isBlank(orderId)) {
                    int updateCount = merchantOrderService.isComment(orderId);
                }

                ResponseUtils.setupSuccessResponse(baseResponse, careWelfareMerchantEvaluationDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCareWelfareMerchantEvaluation(final CareWelfareMerchantEvaluationUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CareWelfareMerchantEvaluation的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CareWelfareMerchantEvaluationDO careWelfareMerchantEvaluationDO = CareWelfareMerchantEvaluationProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = careWelfareMerchantEvaluationService.update(careWelfareMerchantEvaluationDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteCareWelfareMerchantEvaluation(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CareWelfareMerchantEvaluation的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = careWelfareMerchantEvaluationService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<CareWelfareMerchantEvaluationCountDTO> countCareWelfareMerchantEvaluation(CareWelfareMerchantEvaluationSearchRequest request) {

        final BaseResponse<CareWelfareMerchantEvaluationCountDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(request, "CareWelfareMerchantEvaluationSearchRequest的请求参数不能为空");
                BizCheckUtil.checkNotBlank(request.getMerchantId(),"商家ID不能为空");
            }
            @Override
            public void executeService() {
                CareWelfareMerchantEvaluationCountDTO CareWelfareMerchantEvaluationCountDTO = careWelfareMerchantEvaluationService.selectEvaluationCount(request.getMerchantId());
                ResponseUtils.setupSuccessResponse(baseResponse, CareWelfareMerchantEvaluationCountDTO);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_EVALUATION_SEARCH;
            }
        });
        return baseResponse;
    }
}
